package com.arithmeticEveryDay.greed;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

/*

多级调度问题
使用m台相同机器对n个独立作业进行加工处理。每个作业均可在任何一台机器上加工处理。
但不可间断、拆分。多机调度问题要求给出一种作业调度方案，使所给的n个作业在尽可能短的时间内由m台机器加工处理完成。
作业需要随机生成，计算最短时间。
7 3
2 14 4 16 6 5 3

贪心策略一：最短占用时间优先
2 5 16
3 6     ==> 23
4 14

贪心策略二：最长占用时间优先
16
14 3    ==> 17
6 5 4 2


 */
public class MultiMachineSchedule {

    static InOut2 io = new InOut2();
    int n, m;
    Integer[] time;
    
    public MultiMachineSchedule() {
        n = io.nextInt();
        m = io.nextInt();
        time = new Integer[n];
        for (int i = 0; i < n; i++) {
            time[i] = io.nextInt();
        }
    }
    
    private int greed() {
        Comparator<? super Integer> comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                // TODO Auto-generated method stub
                return o2 - o1;
            }
            
        };
        Arrays.sort(time, comparator);
        // io.out.println(Arrays.toString(time));
        if (m >= n) {
            return time[0];
        }
        int[] doing = new int[m];
        int i;
        for (i = 0; i < m; ++i) {
            doing[i] += time[i];
        }
        // io.out.println(Arrays.toString(doing));
        doing[i-1] += time[i++];
        // io.out.println(Arrays.toString(doing));
        while (i < n) {
            int minIndex = 0;
            for (int j = 0; j < m; j++) {
                if (doing[j] < doing[minIndex]) {
                    minIndex = j;
                }
            }
            doing[minIndex] += time[i++];
            // io.out.println(Arrays.toString(doing));
        }
        int maxTime = doing[0];
        for (int j = 0; j < m; j++) {
            if (doing[j] > maxTime) {
                maxTime = doing[j];
            }
        }
        return maxTime;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MultiMachineSchedule m = new MultiMachineSchedule();
        io.out.println(m.greed());
        io.out.flush();
    }

}
class InOut2 {
    BufferedReader br;
    StringTokenizer tok;
    PrintWriter out;
    InOut2() {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(new OutputStreamWriter(System.out));
    }
    public boolean hasNext() {
        while (tok == null || !tok.hasMoreElements()) {
            try {
                tok = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                return false;
            }
        }
        return true;
    }
    public String next() {
        if (hasNext()) {
            return tok.nextToken();
        }
        return null;
    }
    public int nextInt() {
        return Integer.parseInt(next());
    }
}
